#pragma once
#include <string>
#include <iostream>
#include <jsoncpp/json/json.h>
// "len"\n"x op y"\nXXXXXX
const std::string lbreak = "\n";
//const std::string blackspace = " ";
std::string Encode(std::string &content)
{
    std::string package = std::to_string(content.size());
    package += lbreak;
    package += content;
    package += lbreak;
    return package;
}
// "len"\n"x op y"\nXXXXXX
bool Decode( std::string &package, std::string *content)
{
    size_t pos = package.find(lbreak);
    if (pos == std::string::npos)
       return false;
    std::string len_str = package.substr(0, pos);
    size_t len = std::stoi(len_str);
    size_t total_len = len + len_str.size() + 2;
    if (package.size() < total_len)
    return false;
    *content = package.substr(pos + 1, len);
    package.erase(0, total_len);
    return true;

}
class Request
{
public:
    Request(int x, int y, char op)
        : x_(x), y_(y), op_(op)
    {
    }
    Request()
    {}
    ~Request()
    {
    }
    bool Serialize(std::string *out)
    {
        Json::Value root;
        root["x"] = x_;
        root["y"] = y_;
        root["op"] = op_;
        Json::StyledWriter w;
        *out = w.write(root);
        return true;
    }
    bool Deserialize(std::string &in)
    {
        Json::Value root;
        Json::Reader r;
        r.parse(in, root);
        x_ = root["x"].asInt();
        y_ = root["y"].asInt();
        op_ = root["op"].asInt();
        return true;
    }
    void Print()
    {
        std::cout << "构建完成"
                  << " x = " << x_ << " y = " << y_ << " op = " << op_ << std::endl;
    }

public:
    int x_;
    int y_;
    char op_;
};
class Respond
{
public:
    Respond()
    {
    }
    ~Respond()
    {
    }
    bool Serialize(std::string *out)
    {
        Json::Value root;
        root["result"] = result;
        root["code"] = code;
        Json::StyledWriter w;
        *out = w.write(root);
        return true;
    }
    bool Deserialize(std::string &in)
    {
        Json::Value root;
        Json::Reader r;
        r.parse(in, root);
        result = root["result"].asInt();
        code = root["code"].asInt();

        return true;
    }
    void Print()
    {
        std::cout << "计算完成"
                  << "result = " << result << "  code = " << code << std::endl;
    }

public:
    int result = 0;
    int code = 0;
};